package com.txby.pf.common.utils;

import com.jck.common.util.MapSortUtil;
import com.jck.mybatis.util.PageBean;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.xss.SQLFilter;
import com.txby.platformCore.data.CommonMybaitsDataMapExt;

import org.apache.commons.lang.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 查询参数
  */
public class Query extends LinkedHashMap<String, Object> {
	private static final long serialVersionUID = 1L;
	//当前页码
    private int page;
    //每页条数
    private int limit;

    public Query(Map<String, Object> params){
        this.putAll(params);

        //分页参数
        this.page = Integer.parseInt(params.get("page").toString());
        this.limit = Integer.parseInt(params.get("limit").toString());
        this.put("offset", (page - 1) * limit);
        this.put("page", page);
        this.put("limit", limit);

        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        String sidx = (String)params.get("sidx");
        String order = (String)params.get("order");
        if(StringUtils.isNotBlank(sidx)){
            this.put("sidx", SQLFilter.sqlInject(sidx));
        }
        if(StringUtils.isNotBlank(order)){
            this.put("order", SQLFilter.sqlInject(order));
        }

    }
    public static PageBean getPageBean(Map<String, Object> params){
    	//分页参数
    	return new PageBean(Integer.parseInt(params.get("page").toString()), Integer.parseInt(params.get("limit").toString()));
    	
    }
    public static String  getLastSortSql(Map<String, Object> params,String defaultSortSql){
    	if(ChkUtil.isEmpty(defaultSortSql)) defaultSortSql="";
    	String sql="";
    /*	params.remove("page");
    	params.remove("limit");*/
    	 //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        String sidx = (String)params.get("sidx");
        String order = (String)params.get("order");
    	if(!ChkUtil.isEmpty(sidx)&&!ChkUtil.isEmpty(order)){
    		sql+=" order by #{pojo.sidx} #{pojo.order} ";
    	}
    	if(ChkUtil.isEmpty(sql)) sql=defaultSortSql;
		return sql;
    	
    }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
}
